﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTBLL;
using JWBST.CommonClass;

namespace XWG.JWBST.JWBSTManagementApp.GAXX
{

    public partial class WebCollectApply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.RegisterStartupScript("提示", "<script language=javascript>window.history.go(1);</script>");
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            string SerNum = this.texqu.Text.FilteHtml().Trim() + "公（" + this.texjc.Text.FilteHtml().Trim() + "）" + this.texjc0.Text.FilteHtml().Trim() + "字[" + this.texz.Text.FilteHtml().Trim() + "]第" + this.texh.Text.Trim().FilteHtml() + "号";
            TbBstCaseApply mTbBstCaseApply = new TbBstCaseApply();
            mTbBstCaseApply = TbBstCaseApplyBLL.Getapply(SerNum);
            if (mTbBstCaseApply != null)
            {
                if (mTbBstCaseApply.Code == this.yzm.Value)
                {
                    TbBstCaseTodo todo = new TbBstCaseTodo();
                    todo.Applyid = mTbBstCaseApply.Id;
                    todo.Id = Guid.NewGuid().ToString();
                    todo.Movesign = "0";
                    todo.Status = "0";
                    todo.Lastsearchdate = DateTime.Now;
                    TbBstCaseTodoBLL.Insert(todo, "", false);
                    Log(mTbBstCaseApply.Id, "1");
                    Response.Redirect("../GAXX/TbBstReturnForUser.aspx?appid=" + mTbBstCaseApply.Id);
                }
                else
                {
                    this.LabMes.Text = "";
                    this.labMsg.Text = "您填写的验证码错误";
                }
            }

        }

        public void Log(string appid, string opType)
        {
            TbBstCaseLog log = new TbBstCaseLog();
            log.Id = Guid.NewGuid().ToString();
            log.OpDate = DateTime.Now.ToString();
            log.OpType = opType;
            log.Ipaddress = GetWebClientIp();
            log.Applyid = appid;
            TbBstCaseLogBLL.Insert(log, "", false);
        }

        #region 获取web客户端ip
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未获取用户IP";

            try
            {
                if (System.Web.HttpContext.Current == null
            || System.Web.HttpContext.Current.Request == null
            || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    return "";

                string CustomerIP = "";

                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                if (!String.IsNullOrEmpty(CustomerIP))
                    return CustomerIP;

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (CustomerIP == null)
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                }

                if (string.Compare(CustomerIP, "unknown", true) == 0)
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                return CustomerIP;
            }
            catch { }

            return userIP;

        }
        #endregion


        //获取验证码
        protected void btn_askYZM_Click(object sender, EventArgs e)
        {
            //this.yzm.Value;
            string SerNum = this.texqu.Text.FilteHtml().Trim() + "公（" + this.texjc.Text.FilteHtml().Trim() + "）" + this.texjc0.Text.FilteHtml().Trim() + "字[" + this.texz.Text.FilteHtml().Trim() + "]第" + this.texh.Text.Trim().FilteHtml() + "号";
            TbBstCaseApply mTbBstCaseApply = new TbBstCaseApply();
            mTbBstCaseApply = TbBstCaseApplyBLL.Getapply(SerNum);
            if (mTbBstCaseApply != null)
            {
                //update
                ResultModel mResultModel = new ResultModel(); //初始化返回类型
                mTbBstCaseApply.Isaudit = "0";
                mTbBstCaseApply.Movesign = "0";
                mTbBstCaseApply.Code = this.yzm.Value;
                mResultModel = TbBstCaseApplyBLL.Update(mTbBstCaseApply, "WebUser", false);
                if (mResultModel.ISSucceed)
                {
                    this.LabMes.Text = "正在获取验证码。请耐心等待...";
                    this.btn_askYZM.Enabled = false;
                    //控件  不可修改
                    this.texh.ReadOnly = true;
                    this.texjc.ReadOnly = true;
                    this.texqu.ReadOnly = true;
                    this.texz.ReadOnly = true;
                }
            }
            else
            {
                //insert
                ResultModel mResultModel = new ResultModel(); //初始化返回类型
                TbBstCaseApply mmTbBstCaseApply = new TbBstCaseApply();
                mmTbBstCaseApply.Id = Guid.NewGuid().ToString(); //主键
                var dTime = DateTime.Now;
                var lsh = dTime.ToString("yyyyMMdd") + dTime.Millisecond.ToString() + dTime.ToString("hh") + dTime.ToString("ss") + dTime.ToString("mm");
                mmTbBstCaseApply.Username = lsh;
                mmTbBstCaseApply.Tel = DES.Encode(this.textel.Value.FilteHtml());
                mmTbBstCaseApply.Serialnumber = SerNum;
                mmTbBstCaseApply.Createdate = DateTime.Now;
                mmTbBstCaseApply.Isaudit = "0"; //是否验证通过  0 待审核  1 未通过  2通过
                mmTbBstCaseApply.Movesign = "0"; //同步标志
                mmTbBstCaseApply.Ipaddress = GetWebClientIp(); //ip地址
                mmTbBstCaseApply.Code = this.yzm.Value;
                mResultModel = TbBstCaseApplyBLL.Insert(mmTbBstCaseApply, "WebUser", true); //新建操作
                if (mResultModel.ISSucceed)
                {
                    this.btn_askYZM.Enabled = false;
                    this.LabMes.Text = "正在获取验证码。请耐心等待...";
                    //控件  不可修改
                    this.texh.ReadOnly = true;
                    this.texjc.ReadOnly = true;
                    this.texqu.ReadOnly = true;
                    this.texz.ReadOnly = true;
                }
            }
        }
    }
}




////之前申请过  并通过申请。
//if (mTbBstCaseApply.Isaudit == "1")
//{
//    string mtapplyTel = DES.Decode(mTbBstCaseApply.Tel);
//    if (mtapplyTel == this.textel.Value.FilteHtml().Trim())
//    {
//        ResultModel mResultModel = new ResultModel(); //初始化返回类型
//        string Rnum = this.yzm.Value;
//        TbBstSmsSend send = new TbBstSmsSend();
//        send.Pkguid = Guid.NewGuid().ToString();
//        send.Receiver = this.textel.Value.FilteHtml().Trim();
//        send.Content = "您好，您的个案信息申请验证码为：  " + Rnum;
//        send.Sendtype = "1";
//        send.Adddate = DateTime.Now;
//        send.Verification = Rnum;
//        send.Sendtag = "0";
//        send.Movesign = "0";

//        mResultModel = TbBstSmsSendBLL.Insert(send, "", false);
//        if (mResultModel.ISSucceed)
//        {
//            this.LabMes.Text = "验证码已发送，请等待...";
//            this.btn_askYZM.Enabled = false;
//        }
//        else
//        {
//            this.LabMes.Text = "发送失败";
//        }
//    }
//}











//string appid = "";
//           ResultModel mResultModel = new ResultModel(); //初始化返回类型
//           TbBstCaseApply mTbBstCaseApply = new TbBstCaseApply();
//           labMsg.Text = "";
//           string SerNum = this.texqu.Text.FilteHtml().Trim() + "公（" + this.texjc.Text.FilteHtml().Trim() + "）行受字[" + this.texz.Text.FilteHtml().Trim() + "]第" + this.texh.Text.Trim().FilteHtml() + "号";
//           if (txtyanzhengma.Value.FilteHtml().ToString() != this.yzm.Value)
//           {
//               labMsg.Text = "您填写的验证码错误";
//               return;
//           }
//           try
//           {
//               mTbBstCaseApply = TbBstCaseApplyBLL.Getapply(SerNum);
//               if (mTbBstCaseApply != null)
//               {
//                   //update todo 
//                   TbBstCaseTodoBLL.UpdateTodoByapplyid(mTbBstCaseApply.Id);
//                   appid = mTbBstCaseApply.Id;
//                   mResultModel.ISSucceed = true;
//                   Log(appid, "1");
//                   Response.Redirect("../GAXX/TbBstReturnForUser.aspx?appid=" + appid);
//               }
//               else
//               {
//                   TbBstCaseApply mmTbBstCaseApply = new TbBstCaseApply();
//                   mmTbBstCaseApply.Id = Guid.NewGuid().ToString(); //主键
//                   var dTime = DateTime.Now;
//                   var lsh = dTime.ToString("yyyyMMdd") + dTime.Millisecond.ToString() + dTime.ToString("hh") + dTime.ToString("ss") + dTime.ToString("mm");
//                   mmTbBstCaseApply.Username = lsh;
//                   mmTbBstCaseApply.Tel = DES.Encode(this.textel.Value.FilteHtml());
//                   mmTbBstCaseApply.Serialnumber = SerNum;
//                   mmTbBstCaseApply.Createdate = DateTime.Now;
//                   mmTbBstCaseApply.Isaudit = "0"; //是否验证通过  0 待审核  1 未通过  2通过
//                   mmTbBstCaseApply.Movesign = "0"; //同步标志
//                   mmTbBstCaseApply.Ipaddress = GetWebClientIp(); //ip地址
//                   mResultModel = TbBstCaseApplyBLL.Insert(mmTbBstCaseApply, "WebUser", true); //新建操作
//                   appid = mmTbBstCaseApply.Id;

//                   if (mResultModel.ISSucceed)
//                   {
//                       TbBstCaseTodo todo = new TbBstCaseTodo();
//                       todo.Applyid = mResultModel.FiledKey;
//                       todo.Id = Guid.NewGuid().ToString();
//                       todo.Movesign = "0";
//                       todo.Status = "0";
//                       todo.Lastsearchdate = DateTime.Now;
//                       TbBstCaseTodoBLL.Insert(todo, "", false);
//                       Log(appid, "1");
//                       Response.Redirect("../GAXX/TbBstReturnForUser.aspx?appid=" + appid);
//                   }
//               }
//           }
//           catch (Exception ex)
//           {
//               Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交失败\")</script>");
//           }